﻿using Application.Core;
using Application.Interfaces.Caching;
using Application.Interfaces.Repository;
using Domain.Models.Settings;
using System.Linq;
using System.Security.Permissions;

namespace Application.Services
{
    public class SettingService : BaseService, ISettingService
    {
        private ICacheManager _cacheManager;
        private IRepository<Setting> _settingRepo;

        public SettingService(ICacheManager cacheManager, IRepository<Setting> _settingRepo)
        {
            this._cacheManager = cacheManager;
            this._settingRepo = _settingRepo;
        }

        [PrincipalPermission(SecurityAction.Assert)]
        public Setting GetSetting()
        {
            if (_cacheManager.IsSet(CachingKeys.Setting.ToString()))
            {
                return _cacheManager.Get<Setting>(CachingKeys.Setting.ToString());
            }
            var setting = this._settingRepo.Find().FirstOrDefault();
            if (setting != null)
            {
                _cacheManager.Set(CachingKeys.Setting.ToString(), setting);
            }
            return setting;
        }
    }
}